package javaClassExercise;

import java.util.Arrays;

/**
 * Created by lomo on 2017/3/13.
 *
 * 使用comparable 进行集合排序
 */


public class User implements Comparable {

    private String id;
    private int age;

    public User(String id, int age) {
        this.id = id;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int compareTo(Object o) {
        return this.age - ((User) o).getAge();
    }

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        User[] users = new User[] { new User("a", 30), new User("b", 20) };
        Arrays.sort(users);
        for (int i = 0; i < users.length; i++) {
            User user = users[i];
            System.out.println(user.getId() + " " + user.getAge());
        }
    }

}
